import G from "../Utilities/Global";

/**
 * 开场动画
 * created by Five on 2018-12-25 16:17:05
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class UIOpening extends cc.Component {
    // 混沌未分天地乱，茫茫渺渺无人见。
    // 自从盘古破鸿蒙，开辟从兹清浊辨。
    // 覆载群生仰至仁，发明万物皆成善。
    // 欲知造化会元功，敢问前路在何方。
    @property(cc.Node)
    rollingBG: cc.Node = null;

    @property(cc.Node)
    touchPrevent: cc.Node = null;

    @property([cc.Label])
    labels: Array<cc.Label> = new Array<cc.Label>();

    strings: Array<string> = new Array<string>(
        "混沌未分天地乱，茫茫渺渺无人见。",
        "自从盘古破鸿蒙，开辟从兹清浊辨。",
        "覆载群生仰至仁，发明万物皆成善。",
        "欲知造化会元功，敢问前路在何方。",
    );

    start() {
        this.touchPrevent.on(cc.Node.EventType.TOUCH_START, (e: Event) => {
            e.stopPropagation();
        });

        this.rollingBG.runAction(cc.sequence(
            cc.moveBy(10, 0, 694),
            cc.callFunc(() => {
                this.node.removeFromParent();
                G().ToBattleScene(0, false);
            })
        ));

        this.labels.forEach((label => {
            label.string = ""
        }));
        this.filledString(0, 0);
    }

    filledString(index: number, strIndex: number) {
        if (index >= this.labels.length)
            return;
        const label = this.labels[index];
        const str = this.strings[index];
        label.string += str[strIndex];
        if (strIndex < str.length) {
            setTimeout(() => {
                this.filledString(index, strIndex + 1);
            }, 100);
        } else {
            this.filledString(index + 1, 0);
        }
    }
}